package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.EnrollInfo;
import com.xmy.cultivate.entity.EnrollTeam;
import com.xmy.cultivate.entity.EnrollTeamMember;
import com.xmy.cultivate.entity.Staff;
import com.xmy.cultivate.mapper.EnrollTeamMapper;
import com.xmy.cultivate.mapper.EnrollTeamMemberMapper;
import com.xmy.cultivate.mapper.StaffMapper;
import com.xmy.cultivate.service.IEnrollInfoService;
import com.xmy.cultivate.service.IEnrollTeamService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 招生战队 服务实现类
 * </p>
 *
 * @author min
 * @since 2023-02-10
 */
@Service
public class EnrollTeamServiceImpl extends ServiceImpl<EnrollTeamMapper, EnrollTeam> implements IEnrollTeamService {

    @Autowired
    EnrollTeamMemberMapper enrollTeamMemberMapper;

    @Autowired
    StaffMapper staffMapper;

    @Autowired
    EnrollTeamMapper enrollTeamMapper;

    @Autowired
    IEnrollInfoService iEnrollInfoService;

    @Override
    public boolean addBatchMember(EnrollTeam enrollTeam) {
        //this.removeBatchMember(enrollTeam);
        for (Long id:enrollTeam.getTeacherIdList()){
            QueryWrapper<EnrollTeamMember> enrollTeamMemberQueryWrapper = new QueryWrapper<>();
            enrollTeamMemberQueryWrapper.eq("teacher_id",id);
            enrollTeamMemberQueryWrapper.eq("deleted",0);
            enrollTeamMemberQueryWrapper.eq("status",1);
            Long count = enrollTeamMemberMapper.selectCount(enrollTeamMemberQueryWrapper);
            if(count.equals(0L)){
                EnrollTeamMember enrollTeamMember = new EnrollTeamMember();
                enrollTeamMember.setTeamId(enrollTeam.getId());
                enrollTeamMember.setTeacherId(id);
                enrollTeamMember.setStartDate(LocalDate.now());
                enrollTeamMemberMapper.insert(enrollTeamMember);

                UpdateWrapper<EnrollInfo> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("teacher_id",id);
                updateWrapper.ge("add_date",LocalDate.now());
                updateWrapper.set("team_id",enrollTeam.getId());
                iEnrollInfoService.update(updateWrapper);
            }
        }
        return true;
    }

    public boolean removeBatchMember(EnrollTeam enrollTeam){

        for (Long id:enrollTeam.getTeacherIdList()){
           EnrollTeamMember enrollTeamMemberInfo = enrollTeamMemberMapper.selectById(id);

            QueryWrapper<EnrollTeamMember> enrollTeamMemberQueryWrapper = new QueryWrapper<>();
            //enrollTeamMemberQueryWrapper.eq("team_id",enrollTeam.getId());
            enrollTeamMemberQueryWrapper.eq("teacher_id",enrollTeamMemberInfo.getTeacherId());
            enrollTeamMemberQueryWrapper.eq("status",1);

            List<EnrollTeamMember> enrollTeamMemberList = enrollTeamMemberMapper.selectList(enrollTeamMemberQueryWrapper);
            for (EnrollTeamMember enrollTeamMember:enrollTeamMemberList){
                enrollTeamMember.setStatus(0);
                enrollTeamMember.setEndDate(LocalDate.now().plusDays(-1));//截止到上一天
                enrollTeamMemberMapper.updateById(enrollTeamMember);
            }
        }

        return true;
    }

    public boolean delBatchMember(EnrollTeam enrollTeam){
        for (Long id:enrollTeam.getTeacherIdList()){
            EnrollTeamMember enrollTeamMemberInfo = enrollTeamMemberMapper.selectById(id);
            enrollTeamMemberMapper.deleteById(enrollTeamMemberInfo.getId());
        }
        return true;
    }

    @Override
    public boolean setTeamCaptain(String teamId, String teacherId) {
        QueryWrapper<EnrollTeamMember> enrollTeamMemberQueryWrapper = new QueryWrapper<>();
        enrollTeamMemberQueryWrapper.eq("team_id",teamId);
        enrollTeamMemberQueryWrapper.eq("teacher_id",teacherId);
        enrollTeamMemberQueryWrapper.eq("status",1);
        enrollTeamMemberQueryWrapper.eq("is_captain",1);

        List<EnrollTeamMember> enrollTeamMemberList = enrollTeamMemberMapper.selectList(enrollTeamMemberQueryWrapper);
        for (EnrollTeamMember enrollTeamMember:enrollTeamMemberList){
            enrollTeamMember.setIsCaptain(0);
            enrollTeamMember.setEndDate(LocalDate.now());
            enrollTeamMemberMapper.updateById(enrollTeamMember);

            UpdateWrapper<EnrollTeamMember> enrollTeamMemberUpdateWrapper = new UpdateWrapper<>();
            enrollTeamMemberUpdateWrapper.eq("team_id",teamId);
            enrollTeamMemberUpdateWrapper.eq("teacher_id",teacherId);
            enrollTeamMemberUpdateWrapper.set("is_captain",1);
            enrollTeamMemberMapper.update(null,enrollTeamMemberUpdateWrapper);
        }

        return true;
    }

    public List<EnrollTeamMember> getTeamMemberList(String teamId, String name, String status) {
        QueryWrapper<EnrollTeamMember> enrollTeamMemberQueryWrapper = new QueryWrapper<>();
        enrollTeamMemberQueryWrapper.eq("team_id", teamId);
        enrollTeamMemberQueryWrapper.eq(StringUtils.isNotBlank(status), "enroll_team_member.status", status);
        enrollTeamMemberQueryWrapper.eq("enroll_team_member.deleted", 0);
        enrollTeamMemberQueryWrapper.like(StringUtils.isNotBlank(name), "staff.`name`", name);
        enrollTeamMemberQueryWrapper.orderByDesc("enroll_team_member.status");
        List<EnrollTeamMember> enrollTeamMemberList = enrollTeamMemberMapper.getByTeamIdEnrollTeamMembers(enrollTeamMemberQueryWrapper);
        return enrollTeamMemberList;
    }

    public List<EnrollTeam> getTreeList(Long pid,String yearPart,String quarterNum,String status){
        return  enrollTeamMapper.getTreeList(pid,yearPart,quarterNum,status);
    }

    @Override
    public String getChilTeamAtDataName(Long teamId, String startDate, String endDate){
         return enrollTeamMapper.getChilTeamAtDataName(teamId,startDate,endDate);
    }
}
